﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMDTO;
using System.IO;
using System.Text.RegularExpressions;

namespace IMDAL
{
    public class IMDALGeneral
    {

        public string RemarkDescription(int Remarkid, int Languageid)
        {
            // remakr codebook 6
            try
            {

                InMedioEntities db = new InMedioEntities();

                RefCodeDetail s = (from a in db.RefCodeDetail
                             where ( a.idRefCode == 7 && a.refCodeDetail1 == Remarkid &&  a.language == Languageid)
                             select a).FirstOrDefault();

                if (s == null)
                {
                    return "Undefined Error";
                }

                
                return s.description;

            }
            catch (Exception e)
            {
                
                throw e;
            }
            finally
            {
                //Con.close();
            }

        }

        public string GetTranslation(int code, int value, int foreignKey, string foreignvalue, int typeTranslate)
        {
            // type translate 1 : ALbento -> foreign
            // type translate 2 : foreign -> Albento
            try
            {

                InMedioEntities db = new InMedioEntities();

                Translate s = new Translate();
                if (typeTranslate == 1)
                {
                    s = (from a in db.Translate
                            where (a.code == code && a.value == value && a.foreignKey == foreignKey)
                       select a).FirstOrDefault();
                }

                if (typeTranslate == 2)
                {
                    s = (from a in db.Translate
                         where (a.code == code && a.foreignValue == foreignvalue && a.foreignKey == foreignKey)
                         select a).FirstOrDefault();
                }

                if (s == null)
                {
                    return "";
                }

                if (typeTranslate == 1)
                    return s.foreignValue;
                if (typeTranslate == 2)
                    return s.value.ToString();
                return "";

                
            }
            catch (Exception e)
            {
                
                throw e;
            }
            finally
            {
                //Con.close();
            }

        }

        public int ExistingFile(string filenaam)
        {
            try
            {
                if (File.Exists(filenaam))
                {
                    return 1;
                }

                return 0;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                //Con.close();
            }

        }
        static public void ReplaceInFile(string filePath, string searchText, string replaceText)
        {
            try
            {
                StreamReader reader = new StreamReader(filePath);
                string content = reader.ReadToEnd();
                reader.Close();

                content = Regex.Replace(content, searchText, replaceText);

                StreamWriter writer = new StreamWriter(filePath);
                writer.Write(content);
                writer.Close();
            }
            catch (Exception e)
            {

                throw e;
            }
            finally
            {
                //Con.close();
            }

        }
        public string InMedioGuid()
        {
            // Geef GUID terug
            try
            {
                Guid guid = Guid.NewGuid();
                return guid.ToString();
            }
            catch (Exception e)
            {

                throw e;
            }

        }

        public string ControleOndernemingsnr(string ondernemingsnr)
        {
            try
            {
                long ondernemingsnrNum = 0;
                for (int i = 0; i < ondernemingsnr.Length; i++)
                {
                    if (char.IsNumber(ondernemingsnr[i]))
                    {
                        ondernemingsnrNum = long.Parse(ondernemingsnrNum.ToString() + ondernemingsnr[i]);
                    }
                    else
                    {
                        if ((ondernemingsnr[i].ToString() == ".") || (ondernemingsnr[i].ToString() == "B") || (ondernemingsnr[i].ToString() == "E"))
                        {
                        }
                        else
                        {
                            return "-1";
                        }

                    }

                }
                // 10 lang maken
                string terug = ondernemingsnrNum.ToString();

                for (int i = 10; terug.Length < 10; i--)
                {
                    terug = "0" + terug;
                }
                long modulogetal = long.Parse(terug.Substring(1, 7));



                if (terug.Substring(8, 2) == (97 - (modulogetal % 97)).ToString())
                {


                    return terug;
                }
                else
                {
                    return "-1";
                }

            }
            catch (Exception e)
            {
        
                throw e;

            }
        }

        public string GetParameter(int parameter1, int parameter2, int parameter3)
        {
            try
            {
                InMedioEntities db = new InMedioEntities();

                Parameter p = (from a in db.Parameter
                               where (a.parameter1 == parameter1 && a.parameter2 == parameter2 && a.parameter3 == parameter3)
                               select a).FirstOrDefault();
                if (p == null)
                {
                    return "";
                }
                return p.value;

            }
            catch (Exception e)
            {
                
                throw e;
            }
            finally
            {
                //Con.close();
            }

        }




    }
}
